package net.sky.oa.service.system.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sky.oa.dao.system.MyGroupDao;
import net.sky.oa.dao.system.MyGroupEmplDao;
import net.sky.oa.model.MyGroup;
import net.sky.oa.model.MyGroupEmpl;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.service.system.MyGroupEmplService;
import net.sky.oa.util.ConvertUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.web.paging.PagingBean;

@Service
@Transactional
public class MyGroupEmplServiceImpl extends BaseServiceImpl<MyGroupEmpl> implements MyGroupEmplService {
	
	private MyGroupEmplDao dao;
	private MyGroupDao groupDao;

	@Resource
	public void setDao(MyGroupEmplDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}

	@Resource
	public void setGroupDao(MyGroupDao groupDao) {
		this.groupDao = groupDao;
	}

	@Transactional(readOnly=true)
	public List<MyGroupEmpl> findAllGroupEmpl() {
		return this.dao.findAllGroupEmpl();
	}
	
	public List  findMyEmpl(String groupId) {
		return this.dao.findMyEmpl(groupId);
	}
 
	public List<MyGroupEmpl>  findMyEmpl(Long userId,String groupId,String name, String enname, String sex, PagingBean pb) {
		return this.dao.findMyEmpl(userId, groupId, name, enname, sex, pb);
	}
	
	public List  findMyEmpl(Long userId,String name,String sex, String groupId, String school, String dept, String post, PagingBean pb) {
		return this.dao.findMyEmpl(userId, name, sex, groupId, school, dept, post, pb);
	}
	
	public void save(String groupId, String empls) {
		
		List list = this.findMyEmpl(groupId);
		List<Long> ids = ConvertUtils.convertStringToList(empls);
		ids.removeAll(list);
		
		for(Long id : ids){
			MyGroupEmpl entity=new MyGroupEmpl();
			
			MyGroup group  = new MyGroup();
			group.setId( Long.valueOf(groupId) );
			entity.setGroup(group);
			
			SysEmployee employee = new SysEmployee();
			employee.setId( id );
			entity.setUser(employee);
			
			super.save(entity);
		}
	}
	
	public void saveWithGroup(MyGroup group, String empls){
		
		groupDao.save(group);
		
		this.save(String.valueOf(group.getId()), empls);
	}
}